<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ============================================================================
 * $Author: IT宇宙人 2015-08-10 $
 */
namespace Home\Controller;
use Home\Logic\CartLogic;
use Home\Logic\GoodsLogic;
use Think\AjaxPage;
use Think\Page;
use Home\Logic\UsersLogic;
use Think\Verify;
class MessagesController extends BaseController {


    /**
     * 提问
     */
    public function add_message(){
        $user_id=$_SESSION['user']['user_id'];
        if(!$user_id){
            $this->error('Log in before you can operate,Please login',U('Home/User/login'));
        }



        $verify = new Verify();
        if (!$verify->check(I('post.verify_code'),'consult')) {
            $this->error("Verification code error");
        }



        $data=I();
        $data['user_id']=$user_id;
        $data['add_time']=time();
        $data['content']=I('content');
        $res=M('messages')->add($data);
        if(!$res)
            $this->error('Failed to submit, please try again.');
        else
            $this->success('Submitted successfully');
    }




    /**
     * 有用/无用  操作
     */

    public function set_xh(){
        $type=I('type');
        $id=I('id');
        $model=M('messages');
        $model->where("id=$id")->setInc($type); //  积分加1
    }





    /**
     * 问答列表
     */
      public function messages_list(){
          $where['status']=1;
          $where['is_show']=1;
          $order='add_time DESC';
          $count = M('messages')->where($where)->count();
          $page = new Page($count,5);
          $show = $page->show();
          $lists = M('messages')->where($where)->order($order)->limit($page->firstRow.','.$page->listRows)->select();

          foreach($lists AS $k=>$v){
              $v['re_list']=M('messages_re')->where(array('m_id'=>$v[id]))->select();
              $lists[$k]=$v;
          }


          $about=tpCache('about');
          $this->assign('about',$about);



          $this->assign('lists',$lists);
          $this->assign('page',$show);// 赋值分页输出
          $this->display();
      }



    /**
     * 问答回复
     */
    public function re_save(){
        $m_id=I('m_id');
        $data=I();
        $data['m_id']=$m_id;
        $data['add_time']=time();
        $data['user_id']=$_SESSION['user']['user_id'];
        $res=M('messages_re')->add($data);
        if(!$res)
            $this->error('Reply failed, please try again');
        else
            $this->success('Reply success');
    }


    /**
     * 删除回复
     */
    public function delete_re(){
        $where['id']=I('id');
        $res=M('messages_re')->where($where)->delete();
        if(!$res)
            $this->error('Delete failed, please try again');
        else
            $this->success('successfully deleted');
    }


    /**
     * 删除问答
     */
    public function delete_message(){
        $where['id']=I('id');
        $where_re['m_id']=I('id');
        M('messages_re')->where($where_re)->delete();//删除回复
        $res=M('messages')->where($where)->delete();//删除提问
        if(!$res)
            $this->error('Delete failed, please try again');
        else
            $this->success('successfully deleted');
    }

}